package com.kczyjg.hybird

import android.webkit.JavascriptInterface
import androidx.core.graphics.toColorInt
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SizeUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject

class NativeApi(private val webView: H5WebView) {

    @JavascriptInterface
    fun takeNativeAction(jsParam: String) {
        val h5ParamBean: H5ParamBean = GsonUtils.fromJson(jsParam, H5ParamBean::class.java)
        val method = h5ParamBean.method
        val param = h5ParamBean.param
        val callback = h5ParamBean.callback
        when (method) {
            "getLocationInfo" -> getLocationInfo(callback)
            "getVersionInfo" -> getVersionInfo(callback)
            "closeApp" -> closeApp(callback)
            "getStatusBarHeight" -> getStatusBarHeight(callback)
            "setStatusBarLightMode" -> setStatusBarLightMode(callback, param)
            "setStatusBarColor" -> setStatusBarColor(callback, param)
            "transparentStatusBar" -> transparentStatusBar(callback)
        }
    }

    fun getLocationInfo(callback: String) {
        val locationInfo = GaoDeUtils.getLocationInfo()
        val jSONObject = JSONObject().apply {
            put("Latitude", locationInfo[0])
            put("Longitude", locationInfo[1])
            put("Angle", RotationManagerUtil.getYaw())
        }
        val h5ResultBean = H5ResultBean(200, "", jSONObject.toString())
        evaluateJS(callback, h5ResultBean)
    }

    fun getVersionInfo(callback: String) {
        var versionInfo = UpdateApkHelper.getVersionInfo()
        val h5ResultBean = H5ResultBean(200, "", GsonUtils.toJson(versionInfo))
        evaluateJS(callback, h5ResultBean)
    }

    fun closeApp(callback: String) {
        CoroutineScope(Dispatchers.Main).launch {
            ActivityUtils.getTopActivity().finish()
        }
    }

    fun getStatusBarHeight(callback: String) {
        CoroutineScope(Dispatchers.Main).launch {
            val height = BarUtils.getStatusBarHeight()
            val statusBarHeight = SizeUtils.px2dp(height.toFloat())
            val h5ResultBean = H5ResultBean(200, "", statusBarHeight.toString())
            evaluateJS(callback, h5ResultBean)
        }
    }

    fun setStatusBarLightMode(callback: String, param: Array<String>) {
        CoroutineScope(Dispatchers.Main).launch {
            BarUtils.setStatusBarLightMode(ActivityUtils.getTopActivity(), param[0].toBoolean())
            val h5ResultBean = H5ResultBean(200, "", true.toString())
            evaluateJS(callback, h5ResultBean)
        }
    }

    fun setStatusBarColor(callback: String, param: Array<String>) {
        CoroutineScope(Dispatchers.Main).launch {
            BarUtils.setStatusBarColor(ActivityUtils.getTopActivity(), param[0].toColorInt())
            val h5ResultBean = H5ResultBean(200, "", true.toString())
            evaluateJS(callback, h5ResultBean)
        }
    }

    fun transparentStatusBar(callback: String) {
        CoroutineScope(Dispatchers.Main).launch {
            BarUtils.transparentStatusBar(ActivityUtils.getTopActivity())
            val h5ResultBean = H5ResultBean(200, "", true.toString())
            evaluateJS(callback, h5ResultBean)
        }
    }

    fun evaluateJS(callback: String, h5ResultBean: H5ResultBean) {
        CoroutineScope(Dispatchers.Main).launch {
            webView.evaluateJavascript(
                "javascript:$callback('${
                    GsonUtils.toJson(h5ResultBean)
                        .replace("\\", "\\\\")
                }')", null
            )
        }
    }

}